package worldutils.huntgame.AutoStart;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import worldutils.huntgame.HuntGame;
import worldutils.huntgame.Utils.PlayerUtils;
import worldutils.huntgame.Utils.TagUtils;
import worldutils.huntgame.Utils.TimerUtils;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Timer {

    private static JavaPlugin plugin;

    public static void initialize(JavaPlugin pl) {
        plugin = pl;
    }

    private static int taskId = -1;
    private static int timer = 0;

    // 开始计时的方法
    public static void starttick() {
        if (taskId != -1) {
            return; // 如果计时器已经在运行，直接返回
        }

        taskId = new BukkitRunnable() {
            @Override
            public void run() {
                timer++;
                // 每秒执行
                if (TagUtils.getTagCount(HuntGame.getMessage("runner")) + TagUtils.getTagCount(HuntGame.getMessage("hunter")) <= HuntGame.minautostartplayers){
                    stoptick();
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"), HuntGame.getMessage("player-not"));
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"), HuntGame.getMessage("player-not"));
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("spectator"), HuntGame.getMessage("player-not"));
                }


                if (TagUtils.getTagCount(HuntGame.getMessage("runner")) != 0 && TagUtils.getTagCount(HuntGame.getMessage("hunter")) != 0) {
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"), HuntGame.getMessage("start-time-message").replace("*i",String.valueOf(HuntGame.starttime - timer)));
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"), HuntGame.getMessage("start-time-message").replace("*i",String.valueOf(HuntGame.starttime - timer)));
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("spectator"), HuntGame.getMessage("start-time-message").replace("*i",String.valueOf(HuntGame.starttime - timer)));
                } else {
                    stoptick();
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("hunter"), HuntGame.getMessage("start-palyer-error"));
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("runner"), HuntGame.getMessage("start-palyer-error"));
                    PlayerUtils.sendMessageToTaggedPlayersTitle(HuntGame.getMessage("spectator"), HuntGame.getMessage("start-palyer-error"));
                }

//                if (timer == 0) {
//                    PlayerUtils.sendMessageToAllPlayer("1");
//                }
//
//                if (timer == 0) {
//                    PlayerUtils.sendMessageToAllPlayer("1");
//                }

                if (timer == HuntGame.starttime){
                    TimerUtils.startgame();
                    stoptick();
                }
            }
        }.runTaskTimer(plugin, 20L, 20L).getTaskId(); // 每秒执行一次 (20 ticks = 1 second)
    }

    // 停止计时的方法
    public static void stoptick() {
        if (taskId != -1) {
            Bukkit.getScheduler().cancelTask(taskId); // 停止计时器
            taskId = -1; // 重置taskId
            timer = 0; // 重置计时器
        }
    }

    public static boolean getStats() {
        return taskId != -1; // 如果taskId不是-1，说明计时器正在运行
    }
}
